﻿
using HBase; 
using hconnect;
using Net.http;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsSynchro :Synchro {
        static CoreTaskParamStatus[] wcsTaskStatus = new CoreTaskParamStatus[] { CoreTaskParamStatus.满放变更 ,CoreTaskParamStatus.空取 };
        public WcsLoadUp wcsLoad;

        public ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();
      

  
        public virtual MyDict<int ,TaskStatusPersistence> getTaskNo(AbsStatus status) {
            return null;
        }
        protected override void syncDb(AbsStatus cs) {
            MyDict<int ,TaskStatusPersistence> dict = getTaskNo(cs);
            if( dict != null ) {
                updataDB(dict);
            }
        }
        bool isError(int hisStatus) {
            foreach( CoreTaskParamStatus s in wcsTaskStatus ) {
                if( hisStatus == ( int ) s ) {
                    return true;
                }
            }
            return false;

        }

        public virtual void updataDB(MyDict<int ,TaskStatusPersistence> dict) {
            foreach( KeyValuePair<int ,TaskStatusPersistence> kv in dict ) {
                int taskNo = kv.Key;
                if( taskNo == 0 ) {
                    continue;
                }

                TaskStatusPersistence tsp = kv.Value;
                CoreTaskParam param = MyCache.queryUnique<CoreTaskParam>((a) => {
                    return a.taskNo == taskNo && a.type != 9;
                } ,"CompleteParam 过多" + taskNo);
                if( param == null ) {
                    consoleOnly(InfoCode.consleOnly_task_db_null,"无有效任务{1}在站台{0}。" ,tsp.stn ,taskNo);
                    continue;

                }
                int hisStatus = param.status;
                if( isError(hisStatus) ) {
                    consoleOnly(InfoCode.consleOnly_Status_noWnok, "站台{0}的任务{1}状态异常" ,tsp.stn ,taskNo);
                    continue;
                }

                int upStatus = tsp.status;
                if( hisStatus < upStatus ) { 
                    tsp.t.status = (short)tsp.status;
                 
                    coreTaskServer.updateTaskStatus(tsp);//顺序不能变
                    fileAll(InfoCode.consle_status_finsh, className + taskNo + "更新位置:" + tsp.stn + "status" + tsp.status);
                } else {
                    consoleOnly(InfoCode.consle_status_finsh,taskNo + "状态以完成:" + upStatus);
                }
             


            }

        }





    }
}
